/**
 * 初始化商品管理详情对话框
 */
var MerInfoDlg = {
    merInfoData : {},
    categoryZtree: null
};

/**
 * 清除数据
 */
MerInfoDlg.clearData = function() {
    this.merInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
MerInfoDlg.set = function(key, val) {
    this.merInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
MerInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
MerInfoDlg.close = function() {
    parent.layer.close(window.parent.Mer.layerIndex);
}

/**
 * 收集数据
 */
MerInfoDlg.collectData = function() {
    this
    .set('id')
    .set('category')
    .set('mername')
    .set('cost')
    .set('price')
    .set('quantity')
    .set('unit')
    .set('mermodel')
    .set('picture')
    .set('merdesc')
    .set('manufacturer')
    .set('leavefactorydate');
}

/**
 * 提交添加
 */
MerInfoDlg.addSubmit = function() {

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/mer/add", function(data){
        Feng.success("添加成功!");
        window.parent.Mer.table.refresh();
        MerInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.merInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
MerInfoDlg.editSubmit = function() {

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/mer/update", function(data){
        Feng.success("修改成功!");
        window.parent.Mer.table.refresh();
        MerInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.merInfoData);
    ajax.start();
}

MerInfoDlg.showCategorySelectTree = function(){
    Feng.showInputTree("categoryName", "categoryContent");
}
/**
 * 点击类别input框时
 *
 * @param e
 * @param treeId
 * @param treeNode
 * @returns
 */
MerInfoDlg.onClickCategory = function (e, treeId, treeNode) {
    $("#categoryName").attr("value", MerInfoDlg.categoryZtree.getSelectedVal());
    $("#category").attr("value", treeNode.id);
};
MerInfoDlg.onDblClickCategory = function (e, treeId, treeNode) {
    $("#categoryName").attr("value", MerInfoDlg.categoryZtree.getSelectedVal());
    $("#category").attr("value", treeNode.id);
    $("#categoryContent").fadeOut("fast");
};

$(function() {
    var categoryTree = new $ZTree("categoryTree", "/category/tree");
    categoryTree.bindOnClick(MerInfoDlg.onClickCategory);
    categoryTree.bindOnDblClick(MerInfoDlg.onDblClickCategory);
    categoryTree.init();
    MerInfoDlg.categoryZtree = categoryTree;
    // 初始化图片上传
    var avatarUp = new $WebUpload("picture");
    avatarUp.setUploadBarId("progressBar");
    avatarUp.init();
});
